import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from "../views/Home";
import Test1 from "../views/Test1";
import Test2 from "../views/Test2";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Login',
        component: Login,
        hidden: true
    },
    {
        path: '/home',
        name: '导航一',
        component: Home,
        redirect: '/test1',
        children: [
            {
                path: '/test1',
                name: '选项1',
                component: Test1
            },
            {
                path: '/test2',
                name: '选项2',
                component: Test2
            }
        ]
    }
]

const router = new VueRouter({
    mode:'history',
    routes
})

router.beforeEach((to,from,next) => {
    if (to.path === '/'){
        return next();
    }
    const tokenStr = window.sessionStorage.getItem('token');
    if (!tokenStr) {
        alert('未登录，请先登录');
        return next('/');
    }
    next()
})

export default router
